//
//  FocusSquare.m
//  HKOCRuler
//
//  Created by lbxia on 2018/11/23.
//  Copyright © 2018年 lbx. All rights reserved.
//

#import "FocusSquare.h"
#import "Utilities.h"
#import "SCNNode+HKRuler.h"


@interface FocusSquareSegment : SCNNode


- (instancetype)initWithName:(NSString*)name
                       width:(CGFloat)width
                   thickness:(CGFloat)thickness
                       color:(UIColor*)color
                    vertical:(BOOL)vertical;
@end



@implementation FocusSquare


- (instancetype)init
{
    if (self = [super init]) {
        
        self.focusSquareSize = 0.17;
        self.focusSquareThickness = 0.018;
        
        self.scaleForClosedSquare = 0.97;
        
        self.sideLengthForOpenSquareSegments = 0.2;
        
        self.animationDuration = 0.7;
        
        self.focusSquareColor = [UIColor colorWithRed:1 green:205/255. blue:2/255. alpha:1];
        self.focusSquareColorLight = [UIColor colorWithRed:1 green:234/255. blue:112/255. alpha:1];
        
        
        self.opacity = 0.0;
        
        self.lastPosition = SCNVector3Zero;
        self.lastPositionOnPlane = SCNVector3Zero;
        
        self.isOpen = NO;
        self.isAnimation = NO;
    }
    return self;
}


- (SCNNode*)focusSquareNode
{
    CGFloat sl = 0.5;
    CGFloat st = self.focusSquareThickness;
    CGFloat c = self.focusSquareThickness / 2;
    
    
    FocusSquareSegment *s1 = [[FocusSquareSegment alloc]initWithName:@"s1" width:sl thickness:st color:self.focusSquareColor vertical:NO];
    FocusSquareSegment *s2 = [[FocusSquareSegment alloc]initWithName:@"s2" width:sl thickness:st color:self.focusSquareColor vertical:NO];
    FocusSquareSegment *s3 = [[FocusSquareSegment alloc]initWithName:@"s3" width:sl thickness:st color:self.focusSquareColor vertical:YES];
    FocusSquareSegment *s4 = [[FocusSquareSegment alloc]initWithName:@"s4" width:sl thickness:st color:self.focusSquareColor vertical:YES];
    FocusSquareSegment *s5 = [[FocusSquareSegment alloc]initWithName:@"s5" width:sl thickness:st color:self.focusSquareColor vertical:YES];
    FocusSquareSegment *s6 = [[FocusSquareSegment alloc]initWithName:@"s6" width:sl thickness:st color:self.focusSquareColor vertical:YES];
    FocusSquareSegment *s7 = [[FocusSquareSegment alloc]initWithName:@"s7" width:sl thickness:st color:self.focusSquareColor vertical:NO];
    FocusSquareSegment *s8 = [[FocusSquareSegment alloc]initWithName:@"s8" width:sl thickness:st color:self.focusSquareColor vertical:NO];
    
    
    s1.position = [Utilities SCNVector3Add:s1.position right:SCNVector3Make(-(sl / 2 - c), -(sl - c), 0)];
    s2.position = [Utilities SCNVector3Add:s2.position right:SCNVector3Make(sl / 2 - c, -(sl - c), 0)];
    s3.position = [Utilities SCNVector3Add:s3.position right:SCNVector3Make(-sl, -sl / 2, 0)];
    s4.position = [Utilities SCNVector3Add:s4.position right:SCNVector3Make(sl, -sl / 2, 0)];
    s5.position = [Utilities SCNVector3Add:s5.position right:SCNVector3Make(-sl, sl / 2, 0)];
    s6.position = [Utilities SCNVector3Add:s6.position right:SCNVector3Make(sl, sl / 2, 0)];
    s7.position = [Utilities SCNVector3Add:s7.position right:SCNVector3Make(-(sl / 2 - c), sl - c, 0)];
    s8.position = [Utilities SCNVector3Add:s8.position right:SCNVector3Make(sl / 2 - c, sl - c, 0)];
    
    
    SCNPlane *fillPlane = [SCNPlane planeWithWidth:(1.0 - st * 2 + c) height:(1.0 - st * 2 + c)];
    
    SCNMaterial *material = [SCNMaterial materialWithDiffuse:self.focusSquareColorLight respondsToLighting:NO];
    
    fillPlane.materials = @[material];
    
    SCNNode *fillPlaneNode = [SCNNode nodeWithGeometry:fillPlane];
    fillPlaneNode.name = @"fillPlane";
    fillPlaneNode.opacity = 0.0;
    
    SCNNode *planeNode = [[SCNNode alloc]init];
    planeNode.eulerAngles = SCNVector3Make(M_PI / 2.0, 0, 0); // Horizontal
    CGFloat scale = self.focusSquareSize * self.scaleForClosedSquare;
    planeNode.scale = SCNVector3Make(scale, scale, scale);

    [planeNode addChildNode:s1];
    [planeNode addChildNode:s2];
    [planeNode addChildNode:s3];
    [planeNode addChildNode:s4];
    [planeNode addChildNode:s5];
    [planeNode addChildNode:s6];
    [planeNode addChildNode:s7];
    [planeNode addChildNode:s8];
    [planeNode addChildNode:fillPlaneNode];
    
    self.isOpen = NO;
    
    [planeNode renderOnTop];
    
    return planeNode;
}

- (void)hide
{
    if (self.opacity == 1.0) {
        
        [self runAction:[SCNAction fadeOutWithDuration:0.5]];
    }
}

- (void)unhide
{
    if (self.opacity == 0.0) {
        
        [self runAction:[SCNAction fadeInWithDuration:0.5]];
    }
}

//涉及到显示效果，暂时不要
- (void)close:(BOOL)flash
{
//    if (!self.isOpen || self.isAnimation) {
//        return;
//    }
//    
//    self.isAnimation = YES;
    
}

- (void)open
{
    
}

@end


//enum Direction {
//case up
//case down
//case left
//case right
//}

typedef enum : NSUInteger {
    up,
    down,
    left,
    right
} Direction;

@implementation FocusSquareSegment

- (instancetype)initWithName:(NSString*)name
                       width:(CGFloat)width
                   thickness:(CGFloat)thickness
                       color:(UIColor*)color
                    vertical:(BOOL)vertical
{
    if (self = [super init]) {
        
        SCNMaterial* material = [SCNMaterial materialWithDiffuse:color respondsToLighting:NO];
        
        SCNPlane *plane = nil;
        if (vertical) {
            plane = [SCNPlane planeWithWidth:thickness height:width];
        }
        else{
            plane = [SCNPlane planeWithWidth:width height:thickness];
        }
        plane.materials = @[material];
        self.geometry = plane;
        self.name = name;
        
    }
    
    return self;
}

- (void)openWithDirection:(Direction)direction newLength:(CGFloat)newLength
{
    SCNPlane *p = (SCNPlane*)self.geometry;
    if (![p isKindOfClass:[SCNPlane class]]) {
        NSLog(@"%@",@"![p isKindOfClass:[SCNPlane class]]");
        return;
    }

    if (direction == left || direction == right) {
        p.width = newLength;
    }else{
        p.height = newLength;
    }
    
    switch (direction) {
        case left:
        {
            SCNVector3 positon = self.position;
            positon.x = (0.5 / 2 - p.width / 2);
            self.position = positon;
        }
            break;
        case right:
        {
            SCNVector3 positon = self.position;
            positon.x += (0.5 / 2 - p.width / 2);
            self.position = positon;
        }
            break;
        case up:
        {
            SCNVector3 positon = self.position;
            positon.y -= (0.5 / 2 - p.width / 2);
            self.position = positon;
        }
            break;
        case down:
        {
            SCNVector3 positon = self.position;
            positon.y += (0.5 / 2 - p.width / 2);
            self.position = positon;
        }
            break;
            
        default:
            break;
    }
}

- (void)closeWithDirection:(Direction)direction
{
    SCNPlane *p = (SCNPlane*)self.geometry;
    if (![p isKindOfClass:[SCNPlane class]]) {
        NSLog(@"%@",@"![p isKindOfClass:[SCNPlane class]]");
        return;
    }
    
    CGFloat oldLength;
    if (direction == left || direction == right) {
        oldLength = p.width;
        p.width = 0.5;
    }else{
        oldLength = p.height;
        p.height = 0.5;
    }
    
    switch (direction) {
        case left:
        {
            SCNVector3 positon = self.position;
            positon.x -= (0.5 / 2 - oldLength / 2);
            self.position = positon;
        }
            break;
        case right:
        {
            SCNVector3 positon = self.position;
            positon.x += (0.5 / 2 - oldLength / 2);
            self.position = positon;
        }
            break;
        case up:
        {
            SCNVector3 positon = self.position;
            positon.y -= (0.5 / 2 - oldLength / 2);
            self.position = positon;
        }
            break;
        case down:
        {
            SCNVector3 positon = self.position;
            positon.y += (0.5 / 2 - oldLength / 2);
            self.position = positon;
        }
            break;
            
        default:
            break;
    }
}

@end





